home *** CD-ROM | disk | FTP | other *** search
- #ifndef __OBJECTREF__
- #define __OBJECTREF__
- #pragma once
-
- #include "ObjectPtr.h"
-
- //----------------------------------------------------------------------------
- // ObjectReference - a reference to a reference counted object.
- //----------------------------------------------------------------------------
-
- template<class T>
- class SmartRef
- {
- public:
- T& ref;
-
- SmartRef(T* obj) : ref(NewObjectRef(*obj))
- {
- if (ptr == obj)
- TObject::NilObjectReference();
- else
- TObject::NewObjectRef(obj);
- }
- inline SmartRef(T* obj) : ref(obj) { NewObjectRef(*obj); }
- inline SmartRef(T& obj) : ref(obj) { NewObjectRef(obj); }
- inline SmartRef(const SmartRef<T>& ref) : ref(ref.ref) { NewObjectRef(ref); }
-
- // SmartRef(const T& obj) const : ref(&obj) { NewObjectRef(const_cast<TObject*>(&obj)); }
- // SmartRef(T& obj) : ref(&obj) { NewObjectRef(&obj); }
- // SmartRef(const T* obj) const : ref(const_cast<T*>(obj)) { NewObjectRef(const_cast<T*>(obj)); }
-
- #if MEMBER_TEMPLATES_SUPPORTED
-
- template<class U>
- inline SmartRef(SmartRef<U>& rhs) : ref(&rhs) { rhs.NewReference(); }
-
- #else
-
- // SmartRef(TObject* rhs) : ref(dynamic_cast<T*>(rhs)) { NewObjectRef(ref); }
-
- #endif
-
- ~SmartRef()
- {
- TObject::DeleteObjectRef(ref);
- }
-
- operator T&() { return ref; }
-
- #if MEMBER_TEMPLATES_SUPPORTED
-
- inline SmartRef<T>& operator=(const SmartRef<U&> rhs)
- {
- if (this != &rhs)
- {
- ref = SwapObjectRef(ref, rhs);
- }
-
- return *this
- }
-
- #else
-
- inline SmartRef<T>& operator=(const SmartRef<T>& rhs)
- {
- if (ref != rhs.ref)
- {
- TObject::SwapObjectRef(ref, rhs);
- ref = rhs.ref;
- }
-
- return *this;
- }
-
- inline SmartRef<T>& operator=(T& rhs)
- {
- if (ref != rhs)
- {
- TObject::SwapObjectRef(ref, rhs);
- ref = rhs;
- }
-
- return *this;
- }
-
- /*
- SmartRef<T>& operator=(TObject* param)
- {
- T* obj = dynamic_cast<T*>(param);
-
- if (ref != obj)
- {
- SwapObjectRef(ref, obj);
- ref = obj;
- }
-
- return *this;
- }
- */
-
- #endif
-
- inline bool operator==(const T* obj) const
- {
- return ref == obj;
- }
-
- inline bool operator==(const T& obj) const
- {
- return ref == &obj;
- }
-
- inline bool operator==(SmartRef<T>& obj) const
- {
- return ref == obj.ref;
- }
-
- inline bool operator!=(const T& obj) const
- {
- return ref != &obj;
- }
-
- inline bool operator!=(const SmartRef<T>& obj) const
- {
- return ref != obj.ref;
- }
-
- inline bool operator!=(ObjectPtr<T>& obj) const
- {
- return ref != obj.ref;
- }
-
- inline const T* operator&() const
- {
- return &ref;
- }
-
- inline T* operator&()
- {
- return &ref;
- }
- };
-
- /*
- template<class T, class U>
- SmartRef<T>& operator=(SmartRef<U&> rhs)
- {
- if (this != &rhs)
- {
- ref = SwapObjectRef(ref, rhs);
- }
-
- return *this
- }
- */
-
- typedef SmartRef<const TObject> ConstObjectRef;
- //typedef SmartRef<TObject> SmartRef;
-
-
- #endif __OBJECTREF__
-
-